package com.example.demo.controller;

import com.example.demo.model.MainCon;
import com.example.demo.model.News;
import com.example.demo.model.Result;
import com.example.demo.model.User;
import com.example.demo.service.NewsService;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2017/10/8.
 */
@Controller
@RequestMapping(value = "/toutiao")
public class HomeController {

    @Autowired
    private NewsService newsService;

    @Autowired
    private UserService userService;

    /**
     * 访问主页面
     * @return
     */
    @RequestMapping(path = {"/","/index"},method = RequestMethod.GET)
    public String index(){
        return "home";
    }

    @GetMapping("/maincon")
    @ResponseBody
    public Result<MainCon> main(){
        Result<MainCon> result;
        try {
            List<User> users=userService.getAllUser();
            List<News> news=newsService.getLatestNews(0,10);
            MainCon mainCon=new MainCon(users,news);
            result=new Result<MainCon>(true,mainCon);
        }catch (Exception e){
            e.printStackTrace();
            result=new Result<MainCon>(false,e.getMessage());
        }
        return result;
    }

}
